<template>
  <div class="tabs">
    <el-tag type="card" v-for="(item, index) in tags" :key="item.label" :closable="item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'" @click="changeMenu(item)"
            @close="handleClose(item, index)" size="medium">
      {{ item.label }}
    </el-tag>

  </div>
</template>
<script>
import {mapState, mapMutations} from 'vuex'

export default {
  name: 'CommonTag',
  data() {
    return {}
  },
  computed: {
    ...mapState({
      tags: state => state.tab.tabsList
    })
  },
  methods: {
    ...mapMutations(['closeTag']),
    //点击tag跳转的功能
    changeMenu(item) {
      // console.log(item,'item')
      this.$router.push({name: item.name}).catch(() => {
      })
    },
    //点击tag删除
    handleClose(item, index) {

      //调用store中的mutation
      this.closeTag(item)
      const length = this.tags.length
      //删除之后的跳转
      if (item.name !== this.$route.name) {
        return
      }
      //表示的是删除的最后一项
      if (index === length) {
        this.$router.push({
          name: this.tags[index - 1].name
        }).catch(() => {
        })
      } else {
        this.$router.push({
          name: this.tags[index].name
        }).catch(() => {
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.tabs {
  height: 30px;

  .el-tag {
    margin-left: 15px;
  }
}
</style>